// redmothordetail.js
import api from "../../../utils/api"
import util from "../../../utils/util"
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    saved:true,
    hid:1,
    userType:"预约",
    isUser:true,
    showTj:false,
    b_y_id:"",
    userInfo:{

    },
    identifyStatue:"未认证",
    myRole:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    this.setData({
      myRole:app.globalData.userType
    });
    var that = this;
    var id = "";
    if (options.y_id) {
      //用户信息
      id = options.y_id;
      this.setData({
        userType:"牵手",
        isUser:true,
        hid:options.y_id
      });

    } else if (options.h_id) {
      //用户信息
      id = options.h_id;
      this.setData({
        userType: "预约",
        isUser:false
      });
    }
    if (options.b_y_id){
      this.setData({
        b_y_id: options.b_y_id,
        showTj:true
      });
    }
    if(app.globalData.userType == "hn"){
      id = options.y_id;
      this.setData({
        userType: "推荐",
        isUser: true,
      });
    }
    this.setData({
      hid:id
    });
    util.isLogin();
    wx.request({
      url: api.getUserById,
      data:{
        id:id
      },
      success:function(response){
        console.log("获取个人详细信息");
        console.log(response.data.data);
        response.data.data[0].name = util.addStar(response.data.data[0].name)
        response.data.data[0].id = id;
        that.setData({
          userInfo: response.data.data[0]
        });
      },
      method:"POST",
      header:{
        "content-type": "application/x-www-form-urlencoded"
      }
    });
    this.getIdentifyStatue();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    /** 加载个人信息 */
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '自定义转发标题',
      path: '/redmothordetail?id=123',
      withShareTicket:true,
      imageUrl:'../../../img/redmothor_bg.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 收藏红娘
   */
  collect:function(){
    var that = this;
    wx.request({
      url: api.saveRedmothor,
      type:"POST",
      data:{
        id:app.globalData.openid,
        h_id:that.data.hid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        var flag = response.data.infor;
        debugger;
        if(flag == "收藏成功"){
            that.setData({
              saved:true
            });
        }else{
          that.setData({
            saved: false
          });
        }
      }
    })
  },
 /**
  * 预约或牵手
  */
  addmiai:function(index){
    var that = this;
    //用户 牵手
    if(this.data.isUser){
      wx.request({
        // url: api.,
      })
    }else{
      if (this.data.b_y_id){
        /* 推荐 
           执行推荐
        */

       that.tuijian();
       
      }
    //红娘  预约
      wx.request({
        url: api.addmiai,
        data:{
          id:app.globalData.openid,
          b_id: this.data.hid
        },
        header:{
          "content-type": "application/x-www-form-urlencoded"
        },
        method:"POST",
        success:function(res){
          console.log("预约成功！");
          console.log(res.data);
          if (res.data.info == "预约红娘成功"){
            that.setData({
              userType:"取消预约"
            });
          } else if (res.data.info == "取消预约红娘成功"){
            that.setData({
              userType: "预约"
            });
          }
        }
      })
    }
  },
  yuyue:function(){

  },
  qinshou:function(){

  },
  /**
   * 查看相亲表单状态
   */
  querymiai:function(){
    wx.request({
      url: api.querymiai,
      data:{
        id:app.globalData.openid
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log(response.data);
      }
      
    })
  },
  /**
   * 查询是否已认证
   */
  getIdentifyStatue:function(){
    var that = this;
    wx.request({
      url: api.qIdentity_card,
      method:"POST",
      data:{
        id: app.globalData.openid
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      success:function(response){
        console.log("查询身份认证状态：");
        console.log(response.data);
        that.setData({
          identifyStatue:response.data.infor
        });
      }
    })
  },
  /**
   * 推荐
   */
  tuijian:function(){
    this.collection();
    wx.request({
      url: api.message,
      data:{
        u_id:app.globalData.openid,
        r_id: this.data.b_y_id,
        b_id: this.data.hid,
        content:"向您推荐了",
        status:2,
        type:1  //type 为1时表示推荐
      },
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(response){
        console.log("发送消息成功！");
        console.log(response.data);
        
        wx.showToast({
          title: '推荐成功！',
        });
      }
    })
  },
  /**
   * 收藏
   */
  collection(){
    debugger;
    wx.request({
      url: api.saveRedmothor,
      method:"POST",
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      data:{
        id:this.data.hid,
        h_id:this.data.b_y_id
      },
      success:function(response){
        console.log("信息已添加至收藏列表！");
        console.log(response);
      }
    })
  }
})